package Testdemo1;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:周次煜
 * Date: 2024-04-25
 * Time：16:07
 */


public class Test4 {
    public static void duplicateZeros(int[] arr) {
        int cur=0;
        int dest=-1;
        int n=arr.length;
       while (cur < n) {
           if (arr[cur]==0) {
               dest +=2;
           } else {
                dest++;
           }
           if (dest >= n-1) {
               break;
           }
           cur++;

       }

       if (arr[cur]==0) {
           arr[n-1]=0;
           cur--;
           dest-=2;
       }

       while (cur >= 0) {
           if (arr[cur]==0) {
               arr[dest--]=0;
               arr[dest--]=0;
               cur--;
           } else {
               arr[dest--]=arr[cur--];
           }
       }
    }

    public static void main(String[] args) {
        int[]array={1,0,2,3,0,4};
        duplicateZeros(array);
        System.out.println(Arrays.toString(array));
    }
}


